package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduQuestionBankDTO;
import com.zenith.edu.po.vo.EduQuestionBankVO;
import com.zenith.edu.service.EduQuestionBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/question-bank")
@Tag(name = "题库管理", description = "题库管理接口")
public class EduQuestionBankController {
    
    @Autowired
    private EduQuestionBankService eduQuestionBankService;
    
    @SecurePostMapping(value = "/createQuestionBank", roles = "ROLE_ADMIN", permission = "question-bank:create")
    @Operation(summary = "创建题库")
    public Result<EduQuestionBankVO> createQuestionBank(@RequestBody EduQuestionBankDTO dto) {
        EduQuestionBankVO questionBankVO = eduQuestionBankService.createQuestionBank(dto);
        return Result.success(questionBankVO);
    }
    
    @SecurePostMapping(value = "/getQuestionBankById/{id}", roles = "ROLE_ADMIN", permission = "question-bank:read")
    @Operation(summary = "根据ID获取题库详情")
    public Result<EduQuestionBankVO> getQuestionBankById(@PathVariable Long id) {
        EduQuestionBankVO questionBankVO = eduQuestionBankService.getQuestionBankById(id);
        if (questionBankVO == null) {
            return Result.fail("题库不存在");
        }
        return Result.success(questionBankVO);
    }
    
    @SecurePostMapping(value = "/updateQuestionBank", roles = "ROLE_ADMIN", permission = "question-bank:update")
    @Operation(summary = "更新题库信息")
    public Result<EduQuestionBankVO> updateQuestionBank(@RequestBody EduQuestionBankDTO dto) {
        EduQuestionBankVO questionBankVO = eduQuestionBankService.updateQuestionBank(dto);
        return Result.success(questionBankVO);
    }
    
    @SecurePostMapping(value = "/deleteQuestionBank/{id}", roles = "ROLE_ADMIN", permission = "question-bank:delete")
    @Operation(summary = "删除指定题库")
    public Result<Void> deleteQuestionBank(@PathVariable Long id) {
        boolean result = eduQuestionBankService.deleteQuestionBank(id);
        if (!result) {
            return Result.fail("题库删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getQuestionBankList", roles = "ROLE_ADMIN", permission = "question-bank:read")
    @Operation(summary = "获取题库列表")
    public Result<List<EduQuestionBankVO>> getQuestionBankList() {
        List<EduQuestionBankVO> questionBankList = eduQuestionBankService.getQuestionBankList();
        return Result.success(questionBankList);
    }
}